//
//  AZOauthViewController.swift
//  sinaWB
//
//  Created by AzeryKiss on 16/2/25.
//  Copyright © 2016年 AzeryKiss. All rights reserved.
//

import UIKit
import SVProgressHUD

protocol AZOauthDelegate : NSObjectProtocol{
    func loginSuccessMethod()
}


class AZOauthViewController: UIViewController, UIWebViewDelegate {
    
    weak var delegate: AZOauthDelegate?


    let webView = UIWebView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view = webView
        
        setupNavItem()
        
        gogogo()
    }
    
    func gogogo() {
        let url = NSURL(string: "https://api.weibo.com/oauth2/authorize?client_id=\(APP_KEY)&redirect_uri=\(APP_URL)&response_type=code")
        let request = NSURLRequest(URL: url!)
        
        webView.loadRequest(request)
        
        webView.delegate = self
    }
    
    func setupNavItem() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "dissMiss")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: UIBarButtonItemStyle.Plain, target: self, action: "autoGive")
    }
    
    func dissMiss() {
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func autoGive() {
        let js = "document.getElementById(\"userId\").value = \"18511077152\", document.getElementById(\"passwd\").value = \"QWEasdZXC123\""
        webView.stringByEvaluatingJavaScriptFromString(js)
    }
    
    
    
}

extension AZOauthViewController {
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        SVProgressHUD.dismiss()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if let urlString = request.URL?.absoluteString {
            
            if urlString.hasPrefix("http://www.baidu.com"){
                

                //            print(urlString)
                // http://www.baidu.com/?code=e701d35ba90ad78f92e8bf46041f3481
                let urlStr =   urlString.componentsSeparatedByString("=")
                if  let code = urlStr.last {
//                    AZUserViewModel().loadToken(code,sucess:  sucess(),faulse: faulse())
                    AZUserViewModel().loadToken(code, sucess: { () -> () in
                        print("成功")
                        
                        self.presentViewController(AZWelcomeViewController(), animated: false, completion: { () -> Void in


//                            self.delegate?.loginSuccessMethod()
                        })
                        
                        
                        }, faulse: { () -> () in
                            print("失败")

                    })
                }
                return false
            }
        }
        
        return true
    }
    

    
}

